package com.itheima.common.shiro.realm;

import com.google.gson.Gson;
import com.itheima.common.shiro.entity.UserInfo;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import java.util.Set;

/**
 * @author qiankaofei
 * @version 1.0
 * @date 2020/11/14 13:37
 */
public class IhrmRealm extends AuthorizingRealm {
    protected static final String JWTSTR="Bearer ";


    /**
     * 设置Realm域名称
     */
    public void setName(String name) {
        super.setName("ihrmRealm");
    }

    /**
     * 授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取安全数据
        String userInfoJson = (String) principalCollection.getPrimaryPrincipal();
    //userInfoJson转UserInfo对象
    Gson gson=new Gson();
    UserInfo userInfo = gson.fromJson(userInfoJson, UserInfo.class);
        if(userInfo!=null){
        //获取权限信息中的apis
        Set<String> apis = (Set<String>) userInfo.getRoles().get("apis");
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        info.setStringPermissions(apis);
        return info;
    }
        return null;
}

    /**
     * 认证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        return null;
    }
}
